package com.jcfk.eam.controller.mould;

import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.*;
import com.jcfk.eam.domain.dto.base.EamPlanRepairOrderDTO;
import com.jcfk.eam.domain.query.base.EamPlanRepairOrderQuery;
import com.jcfk.eam.enums.AssetKind;
import com.jcfk.eam.service.main.EamPlanRepairOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 模具计划修工单
 *
 * @author yrl
 * @email 
 * @date 2024-12-10
 */
@Api(tags = {"模具计划修工单管理"})
@RestController
@RequestMapping("mouldplanrepairorder")
public class MouldPlanRepairOrderController {
    @Autowired
    private EamPlanRepairOrderService eamPlanRepairOrderService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamPlanRepairOrderQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询计划修工单")
    @PostMapping("/queryByPage")
    public RetPage<EamPlanRepairOrderDTO> queryByPage(@RequestBody ReqPageParam<EamPlanRepairOrderQuery> requestParam) {
        if(requestParam.getSort().getOrderItem()==null){
            ReqSortParam<EamPlanRepairOrderQuery> sortParam=new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }
        DataPage<EamPlanRepairOrderDTO> page = new DataPage<EamPlanRepairOrderDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            EamPlanRepairOrderDTO.class);
        requestParam.getData().setAssetKind(AssetKind.MOULD.getValue());
        page = eamPlanRepairOrderService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     *  计划修工单
     * 根据id 查询详情
     */
    @ApiOperation("查询计划修工单")
    @GetMapping("/get/{id}")
    public RetResult<EamPlanRepairOrderDTO> get(@PathVariable("id") String id) {
            EamPlanRepairOrderDTO item = eamPlanRepairOrderService.get(id);
        return RetResult.ok(item);
    }

    /**
     *  批量删除计划修工单
     */
    @ApiOperation("批量删除计划修工单")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "计划修工单管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamPlanRepairOrderDTO eamPlanRepairOrder) {
        return RetResult.ok(eamPlanRepairOrderService.logicDeleteList(eamPlanRepairOrder));
    }

    /**
     *  删除计划修工单
     */
    @ApiOperation("删除计划修工单")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "计划修工单管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamPlanRepairOrderService.deleteItem(id);
        return RetResult.ok();
    }


    // -------------------------------------------------------PDA----------------------------------------------------------

    /**
     * 根据当前登录用户获取工单列表
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamPlanRepairOrderQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "根据当前登录用户获取工单列表", notes = "根据当前登录用户获取工单列表", httpMethod = "POST")
    @PostMapping("getListByLoginUser")
    public RetPage<EamPlanRepairOrderDTO> getList(@RequestBody ReqPageParam<EamPlanRepairOrderQuery> requestParam) {

        String userId = SecurityUtils.getUserId();
        requestParam.getData().setUserId(userId);

        requestParam.getData().setAssetKind(AssetKind.MOULD.getValue());

        DataPage<EamPlanRepairOrderDTO> page = new DataPage<EamPlanRepairOrderDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                EamPlanRepairOrderDTO.class);

        page = eamPlanRepairOrderService.queryMyList(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 开始维修
     */
    @ApiOperation("开始维修")
    @PostMapping("/start")
    public RetResult<?> start(@RequestBody @Valid EamPlanRepairOrderDTO eamPlanRepairOrder) {
        // 更新
        eamPlanRepairOrderService.start(eamPlanRepairOrder);
        return RetResult.ok();

    }
    /**
     * 添加/更新管理计划修工单
     */
    @ApiOperation("保存计划修工单")
    @PostMapping("/save")
    @RequestLogHeader(modular = "计划修工单管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamPlanRepairOrderDTO eamPlanRepairOrder) {
        // 更新
        eamPlanRepairOrderService.update(eamPlanRepairOrder);
        return RetResult.ok();

    }

    /**
     * 维修完成
     */
    @ApiOperation("维修完成")
    @PostMapping("/finish")
    public RetResult<?> finish(@RequestBody @Valid EamPlanRepairOrderDTO eamPlanRepairOrder) {
        // 更新
        eamPlanRepairOrderService.finish(eamPlanRepairOrder);
        return RetResult.ok();

    }
}
